package controller;

import java.security.acl.Group;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import model.BounBookUser;
import model.BounGroup;
import model.GroupMembers;


import util.HibernateUtil;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class GroupController extends ActionSupport implements ServletRequestAware{
	private static final long serialVersionUID = 1L;
	
	private List<BounGroup> groupList = new ArrayList<BounGroup>();
	private BounGroup showGroup = new BounGroup();
	private List<Object> showMember = new ArrayList<Object>();
 

	
	private String searchName = "";
	private int groupid = 0;

	
	
	
	


	public String list() throws Exception {
		Dictionary<String, Object> dictionary = new Hashtable<String, Object>();
		if ( ! searchName.equals(NONE)) {
			dictionary.put("search", "%" + searchName + "%");
		}else{
			dictionary.put("search", "%");
		}
	
		
		groupList = HibernateUtil.list("from BounGroup b where (groupname like :search) OR (description like :search)", dictionary);

		return SUCCESS;

	}
	
	
	public String show() throws Exception {
	
		Dictionary<String, Object> parameters = new Hashtable<String, Object>();
		parameters.put("groupid", groupid);
		
		showGroup = HibernateUtil.get(BounGroup.class, groupid);
		List<GroupMembers> list = HibernateUtil.list("from GroupMembers where :groupid = groupid", parameters);
		
		for (GroupMembers member : list) {
			showMember.add(HibernateUtil.get(BounBookUser.class, member.getUserid()));
		}
		
		return SUCCESS;

	}
	

					
	
	
	public String createGroupMember() throws Exception {
		GroupMembers groupMembers = new GroupMembers(); 
		Map<String, Object> session = ActionContext.getContext().getSession();
	    groupMembers.setUserid(Integer.parseInt(session.get("userId").toString()));
	    groupMembers.setGroupid(groupid);
		HibernateUtil.insert(groupMembers);
		
		Dictionary<String, Object> parameters = new Hashtable<String, Object>();
		parameters.put("groupid", groupid);
		List<GroupMembers> list = HibernateUtil.list("from GroupMembers where :groupid = groupid", parameters);
		
		for (GroupMembers member : list) {
			showMember.add(HibernateUtil.get(BounBookUser.class, member.getUserid()));
		}
		
		showGroup = HibernateUtil.get(BounGroup.class, groupid);
		
		
		return SUCCESS;
	}

	
	

	public String create() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		showGroup.setOwnerid(session.get("userId").toString());
		
		boolean userTypeStaff = (request.getParameter("userTypeStaff") == null ? false: true);
		boolean userTypeAlumni = (request.getParameter("userTypeAlumni") == null ? false: true);
		boolean userTypeStudent = (request.getParameter("userTypeStudent") == null ? false: true);
		
		int auth = 0;
		if (userTypeStaff)
		{
			auth = auth +  1;
		}
		else if (userTypeAlumni)
		{
			auth = auth + 2;
		}
		else if (userTypeStudent)
		{
			auth = auth + 4;
		}
		
		showGroup.setAuthorizedUserTypes(auth);
		
		HibernateUtil.insert(showGroup);
		list();
		return SUCCESS;
	}

	public List<BounGroup> getGroupList() {
		return groupList;
	}

	public void setGroupList(List<BounGroup> groupList) {
		this.groupList = groupList;
	}

	public String getSearchName() {
		return searchName;
	}

	public void setSearchName(String searchName) {
		this.searchName = searchName;
	}


	public BounGroup getShowGroup() {
		return showGroup;
	}


	public void setShowGroup(BounGroup showGroup) {
		this.showGroup = showGroup;
	}


	public int getGroupid() {
		return groupid;
	}


	public void setGroupid(int groupId) {
		this.groupid = groupId;
	}

	public HttpServletRequest request;
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
		
	}


	public List<Object> getShowMember() {
		return showMember;
	}


	public void setShowMember(List<Object> showMember) {
		this.showMember = showMember;
	}



}
